/*==============================================================================
ROBUSTNESS OF WAGE ESTIMATION 

Data 	: B1_age55data.dta.
Folder 	: Jonas/data/B1_age55data.dta
Date	: 2019-11-26

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	
==============================================================================*/

clear
set more		 off
cap   log close 	_all

log using 	"../log/B2_wages_robust.log"	, replace 
use		 "$datapath/A3_age55data_WC.dta"

{ // Indicate first observations of new job
*===============================================================================
*Condtion on newfirm==1 and observred wage (post eventtime>=0 by construction)
bys lopnr (eventtime_def) : gen nvals = sum(newfirmid!=. & manl_newfirm!=. & newfirmid==firstnewfirmid) if newfirmid!=. & manl_newfirm!=. & newfirmid==firstnewfirmid
gen xnvalsdate = date if nvals==1 
bys lopnr (eventtime_def) : egen nvalsdate = max(xnvalsdate) // impute for all obs this value
format nvalsdate %tm

/*
*Generte same as above but correct for observing the wage possibly earlier
bys lopnr (eventtime) : gen nvals2 = sum(1) if firmid_lonestruktur==firstnewfirmid & eventtime_def>=0 & firmid_lonestruktur!=.
gen xnvals2date = date if nvals2==1 
bys lopnr (eventtime_def) : egen nvals2date = max(xnvals2date) // impute for all obs this value
format nvals2date %tm

*Indicate first wage from new firm in wage survey (no matter the firm and RAMS indicators)
bys lopnr (eventtime) : gen nvals4 = sum(1) if firmid!=firmid_lonestruktur & firmid_lonestruktur!=. & eventtime_def>=0
*/


*Pre wage from notifying firm (last observred wage at notifying firm before or at notification)
bys lopnr (eventtime_def) : gen nvals3 = _n if manl!=. & date<=nvalsdate & eventtime_def<=0
bys lopnr (eventtime_def) : egen maxnvals3 = max(nvals3)


*Date for last wage at notifying firm (within 2 yers before notification)
gen xnvals3date = date if nvals3==maxnvals3	& nvals3!=. & inrange(eventtime_def,-24,0)
bys lopnr (eventtime_def) : egen nvals3date = max(xnvals3date)
format nvals3date %tm

*Difference in time between old and new wage
gen diff = nvalsdate - nvals3date
sum diff if nvals==1

*Wage at notifying firm	
gen xprewage = manl if date==nvals3date 
bys lopnr (eventtime_def) : egen prenotfwage = max(xprewage)
drop xprewage

*fixa sa att datumet for gamla lonen inte far vara efter nya jobbet fas (i.e date of nvals3 < date of nvals)

*Generate eventtime for wage growth (i.e time after having found new job)
bys lopnr (eventtime_def) : gen timeafterjob = sum(1) if nvals>=1 & date>= nvalsdate


}
*

*Generate indicator for fiding a job within 24 months after notice
bys lopnr (eventtime_def) : egen within24months = max(nvals==1 & inrange(eventtime_def,0,24))



{ // Generate wages & Pr(fulltime)
*===============================================================================
*Generate log wage variables 
gen log_manl_newfirm = log(manl_newfirm)
bys lopnr (eventtime_def) : gen preobswage= manl_firstnewfirmid[_n-12]!=. & nvals==1 & eventtime_def[_n-12]>=0
bys lopnr (eventtime_def) : replace log_manl_newfirm = log(manl_firstnewfirmid[_n-12]) if preobswage==1
gen diff_log_manl_newfirm = log(manl_newfirm) - log(prenotfwage)
gen log_prenotfwage = log(prenotfwage)


gen manl_newfirm2 = manl_notfirm_specific if firmid_lonest!=firmid
gen log_manl_newfirm2 = log(manl_newfirm2)
gen diff_log_manl_newfirm2 = log(manl_newfirm2) - log(prenotfwage)
*Generate wage growth per year
gen firstnewwage = manl_newfirm if nvals==1
bys lopnr (eventtime_def) : egen fill_manl_newfirm = max(firstnewwage) 
bys lopnr (eventtime_def) : gen wgrowth = log(manl_newfirm) - log(fill_manl_newfirm)


merge m:1 persid year using "$datapath/A1_clean_wagesurvey_00_19_persidyear.dta", keepusing(tjomf* arbtim*)
drop if _merge==2
drop _merge

gen a = tjomf_notfirm_specific if firmid_lonestruktur!=firmid
gen b = arbtim_notfirm_specific if firmid_lonestruktur!=firmid
gen fulltime = (a>=100 | b>=138) if a!=. | b!=.

}
*
{ // Define globals
*===============================================================================
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_tenureatnot C_educ1 C_educ2 C_educ3 "	
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*
bys lopnr  : gen eventyear = year(dofm(notdate_def)) 
replace eventyear = year - eventyear

bys lopnr (eventtime) : gen x_firmid_lonestruktur_tm1 = firmid_lonestruktur if eventyear==-1
bys lopnr (eventtime) : egen firmid_lonestruktur_tm1 = max(x_firmid_lonestruktur_tm1)
drop x_firmid_lonestruktur_tm1

keep if inrange(eventtime_def,0,48)
bys lopnr (eventtime_def) : gen nvals2 = sum(1) if log_manl_newfirm2!=.
gen same = firmid_lonestruktur_tm1==firmid_lonestruktur & nvals2==1


bys lopnr (eventtime_def): carryforward E1, gen(E1carry)

bys lopnr (eventtime_def) : gen indicate = sum(E1carry!=.) 
replace indicate =. if indicate>1
bys lopnr (eventtime_def) : gen noteq =  E1carry!=E1carry[_n+1] if E1carry!=. & E1carry[_n+1]!=.
replace noteq = indicate if indicate!=.
bys lopnr (eventtime_def) : gen noteqsum = sum(noteq) 
gen noteqsum_replace = cond(noteqsum==0,1 ,noteqsum)
gen observejob = noteqsum>0

*here we go
gen first=firstnewfirmid==firmid_lonestruktur if nvals2==1  & !mi(firmid_lonestruktur)
STOP

*bys lopnr (eventtime_def): egen findjob24 = max(otherfirm2) if eventtime_def<=24

{ // ORIGINAL RD graph  
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm"
	local share = 5
	keep if nvals==1 & eventtime_def<=24
	
	gen share = (manl_newfirm-prenotfwage)/prenotfwage < -.0`share' 		if prenotfwage!=.
	sum share
	*winsor2 `outcome', c(5 100) replace
*=========
xtset ym_def
*=========

	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)

	
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 
	
	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		  text(10.26 -6 "{&beta}=0`b'**") text(10.255 -6 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars.pdf", replace
restore	
}
*


{ // LARGE SAMPLE RD graph: Wage AT FIRST NEW JOB graph 
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24 
	
	xtset ym_def
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(a,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		    text(10.23 -10 "{&beta}=0`b'**")  text(10.223 -10 "(0`se')") 
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Wage AT FIRST NEW JOB graph  DOUNUT
*===============================================================================
local exclude = 3
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24 
	
	gen runvar2 = round(runvar*12)
	drop if inrange(runvar2,-`exclude',`exclude')
	
	
	xtset ym_def
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(a,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		    text(10.23 -10 "{&beta}=0`b'**")  text(10.223 -10 "(0`se')") 
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_donutRD.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Wage AT FIRST NEW JOB graph (winsorize)
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24 
	
	xtset ym_def
	
	winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(a,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		    text(10.23 -10 "{&beta}=0`b'**")  text(10.225 -10 "(0`se')") ///
		    note("Note: winsozired at p1 p99 in sample by year",size(small))
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_w_1_99.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Wage AT FIRST NEW JOB graph and GET RID OF OLD JOBS (defined by wage survey)
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24 
	drop if same==1 	// 119 obs.
	
	xtset ym_def
	
	*winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 	///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 	///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 				///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 				///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") 				///
		    text(10.23 -10 "{&beta}=0`b'**")  text(10.225 -10 "(0`se')") 			///
		    yscale(r(10.16 10.26)) ylab(10.16(.05)10.26)
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_nooldjob.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Wage AT FIRST NEW JOB graph (winsorize) GET RID OF OLD JOBS (defined by wage survey)
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24 
	drop if same==1 	// 119 obs.
	
	*keep if fulltime==1
	xtset ym_def
	
	winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 	///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 	///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 				///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 				///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") 				///
		    text(10.23 -10 "{&beta}=0`b'**")  text(10.225 -10 "(0`se')")  			///
		    yscale(r(10.16 10.26)) ylab(10.16(.05)10.26)
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_w_1_99_nooldjob.pdf", replace
restore	
}
*


{ // LARGE SAMPLE RD graph: Pr(full-time) GET RID OF OLD JOBS 
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"fulltime"
	keep if nvals2==1 & eventtime_def<=24 
	drop if same==1 	// 119 obs.
	xtset ym_def
	

	*winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.003)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 	///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 	///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 				///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 				///
		  xtitle("Normalized age (months)") ytitle("Pr(full-time work)") 				///
		    text(0.85 -24 "{&beta}=0`b'*")  text(0.842 -24 "(0`se')") 		
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_nooldjob.pdf", replace
restore	
}
*
{ // LARGE SAMPLE DONUT RD graph: Wage AT FIRST NEW JOB graph  (winsorize) GET RID OF OLD JOBS (defined by wage survey)
*===============================================================================
local exclude = 2


preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm2"
	keep if nvals2==1 & eventtime_def<=24
	drop if same==1 	// 119 obs.
		
	winsor2 `outcome', c(1 99) replace
	gen runvar2 = round(runvar*12)
	drop if inrange(runvar2,-`exclude',`exclude')
*=========
xtset ym_def
*=========

	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b 	= round(_b[$instrument] 	,0.001)
	local se 	= round(_se[$instrument] 	,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 
	
	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	*tab runvar2 bin4
	
	
	collapse (mean) `outcome' `outcome'_bin    (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		  text(10.24 -12 "{&beta}=0`b'*")  text(10.235 -12 "(0`se')") 
		 * text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_DONUT_w1_99_nooldjob_ex`exclude'.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Number of employers until first new wage observation
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"noteqsum"
	keep if nvals2==1 & eventtime_def<=24 
	*drop if same==1 	// 119 obs.
	xtset ym_def
	

	*winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 	///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 	///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 				///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 				///
		  xtitle("Normalized age (months)") ytitle("# of uniqe (highest paying) jobs between notification" "and wage observation") 				///
		    text(2.08 -20 "{&beta}=0`b'*")  text(2.075 -20 "(0`se')")				///
		    note("Notes: # of jobs is defined as (the [potenially two multiple] highest paying each month) new jobs"  ///
			 "with on average earnings above 10K in a month.", size(small))
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_nooldjob.pdf", replace
restore	
}
*
{ // LARGE SAMPLE RD graph: Pr that first new wage observation is from the first observed new firm in RAMS
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"first"
	keep if nvals2==1 & eventtime_def<=24 
	*drop if same==1 	// 119 obs.
	xtset ym_def
	

	*winsor2 `outcome' ,replace c(1 99) by(year)
	
	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 

	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 	///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 	///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 				///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 				///
		  xtitle("Normalized age (months)") ytitle("Pr(wage paying firm is first new firm in RAMS)") 				///
		    text(.8 -12 "{&beta}=0`b'**")  text(.79 -12 "(0`se')")				///
		    note("Notes: THe probability includes that the first new firm in RAMS is unobservered. N=3,932." ///
		    "Also, a new firm from RAMS is denfied by the 10K threshold and not working there before notif." , size(small))
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_nooldjob.pdf", replace
restore	
}
*


{ // ORIGINAL SAMPLE WAGE CUT RD graph : SAHRE OF OLD Wage AT FIRST NEW JOB graph 
*===============================================================================
preserve
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"share"
	local share = 5
	keep if nvals==1 & eventtime_def<=24
	
	gen share = (manl_newfirm-prenotfwage)/prenotfwage < -.0`share' 		if prenotfwage!=.
	sum share
	*winsor2 `outcome', c(5 100) replace
	format %09.3f `outcome' 
*=========
xtset ym_def
*=========

	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	*local b = _b[$instrument]
	*local se = _b[$instrument]
	local b = abs(round(_b[$instrument] ,0.009))
	local se = round(_se[$instrument], 0.009)
	*local b  : di %9.3f _b[$instrument] 
	*local se : di %9.3f _se[$instrument] 
	
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 
	
	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
		
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(share_`share',replace) 					///
		  xtitle("Normalized age (months)") ytitle("Pr(wage cut > `share'%)") ///
		    text(.55 10 "{&beta}=-0`b'**")  text(.54 10 "(0`se')") 
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_wagecut_`share'percent_covars.pdf", replace
restore	
}
*
{ // ORIGINAL SAMPLE WAGE INCREASE RD graph: SAHRE OF OLD Wage AT FIRST NEW JOB graph 
*===============================================================================
preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"increase_w"
	local share = 5
	keep if nvals==1 & eventtime_def<=24
	
	gen increase_w = (manl_newfirm-prenotfwage)/prenotfwage > .0`share' if prenotfwage!=.
	*winsor2 `outcome', c(5 100) replace
*=========
xtset ym_def
*=========

	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b = round(_b[$instrument] ,0.001)
	local se = round(_se[$instrument] ,0.001)
	
	*local b  : di %-9.3f _b[$instrument] 
	*local se : di %-9.3f _se[$instrument] 
	
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 
	
	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	
	
	collapse (mean) `outcome' `outcome'_bin      (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(share_`share',replace) 					///
		  xtitle("Normalized age (months)") ytitle("Pr(wage increase > `share'%)") ///
		    text(.18 7 "{&beta}=0`b'**")  text(.17 7 "(0`se')") 
		  *text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_`share'percent_covars.pdf", replace
restore	
}
*
{ // ORIGINAL SAMPLE DONUT RD graph orignial sample: Wage AT FIRST NEW JOB graph 
*===============================================================================
local exclude = 3


preserve
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	local outcome 	"log_manl_newfirm"
	keep if nvals==1 & eventtime_def<=24
	
	*winsor2 `outcome', c(5 95) replace
	gen runvar2 = round(runvar*12)
	drop if inrange(runvar2,-`exclude',`exclude')
*=========
xtset ym_def
*=========

	*Main regression (1)
	xtreg `outcome'	$instrument $linear $covariets	 , cluster(varselid) fe nonest 
	local b 	= round(_b[$instrument] 	,0.001)
	local se 	= round(_se[$instrument] 	,0.003)
	keep if e(sample)==1
	gen freq=1
	
	*Residulize year*moth fixed effects
	xtreg `outcome'   $covariets  if e(sample)==1, cluster(varselid) fe nonest 
	predict e, e
	replace `outcome' = e  + _b[_cons]

	*Should be numerically equivelent to (1)
	xtreg `outcome' 	$instrument $linear 	 , cluster(varselid) fe nonest 
	
	*Fix bins of runvar
	gen 	bin = round(runvar*12)
	egen 	bin2 = cut(bin) , at(-36(2)1)
	egen 	bin3 = cut(bin), at(1(2)36)
	gen 	bin4 = bin2
	replace bin4 = bin3 if bin4==.
	replace bin4 = bin4 + 0.5
	bys bin4: egen `outcome'_bin = mean(`outcome')
	*tab  runvar bin4
	*tab runvar2 bin4
	
	
	collapse (mean) `outcome' `outcome'_bin    (sum) freq , by(runvar bin4 treat)
	
	*Change scale of runvar
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin4 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	 name(b,replace) 					///
		  xtitle("Normalized age (months)") ytitle("log(new wage)") ///
		  text(10.26 -12 "{&beta}=0`b'*")  text(10.255 -12 "(0`se')") 
		 * text(-.115 -24 "{&beta}=0`b'") text(-.12 -24 "(0`se')") 

	graph export "$agegraphpath/RD/RD_`outcome'_covars_donutRD_ex`exclude'.pdf", replace
restore	
}
*
* See also B4_wage_RDpermutation.do

log close
